<?php
 include "counter2.php";

pagestart();
?>
<?php pagehead(_("ADM: Display person") . " $user") ?>
<BODY bgcolor="white">
<?php pagetop() ?>
<?php
iamashowpage();
showmessage();

$user = $counter->get_input("user", "number", "get");
$u = new User($user);


?>

<h1><?php echo "$user ", $u->pfields[name] ?></h1>
<?php

showstate($u);
?>
<form method="get" action="/cgi-bin/adm/useractions.cgi">
<input type="hidden" name="user" value="<?php echo $user ?>">
Actions:
<?php if ($u->ufields[state] == "frozen") { ?>
<?php print "<input type=\"submit\" name=\"act.edit\" value=\"" . _("Thaw & Edit") . "\">" .
"<input type=\"submit\" name=\"act.research\" value=\"" . _("Research!") . "\">" .
"<input type=\"submit\" name=\"act.untraceable\" value=\"" . _("Unfindable") . "\">" .
"<input type=\"submit\" name=\"act.nolinux\" value=\"" . _("Not a Linux user") . "\">" .
"<input type=\"submit\" name=\"act.drop\" value=\"" . _("I give up") . "\">"; ?>
<?php } else { ?>
<?php print "<input type=\"submit\" name=\"act.edit\" value=\"" . _("Edit") . "\">" .
"<input type=\"submit\" name=\"act.freeze\" value=\"" . _("Freeze") . "\">"; ?>
<?php } ?>
</form>
<?
showdates($u);
if ($u->hasperson()) {
   showperson($u);

   print "<h3>" . _("Geography") . "</h3>\n";
   showplace($u);
}
print "<h3>" . _("Emails") . "</h3>\n";
showemails($u);

if ($u->ufields[state] == "frozen") {
   showfrozen($u);
}

if ($u->ufields[login]) {
   showmaintainedplaces($u);
}


?>
<?php pagebottom("yes") ?>
</body>
</html>


<?php
function showstate($u) {
	print _("State") . ": <b>" . $u->ufields[state] . "</b>";
	 if ($u->ufields[comment]) {
      		print "<br>" . _("Comment") . ": " . $u->ufields[comment];

	}
}

function showperson($u) {

print "<h3>" . _("Person record") . "</h3>" ?>
<table>
<tr><td><?php print _("Name") ?><td><?php echo $u->pfields[name] ?></tr>
<tr><td><?php print _("Homepage") ?><td><?php echo $u->pfields[homepage] ?></tr>
<tr><td><?php print _("Started") ?><td><?php echo $u->pfields[started] ?></tr>
<tr><td><?php print _("Usage") ?><td><?php echo $u->pfields[usage] ?></tr>
<tr><td><?php print _("May publish") ?><td><?php echo $u->pfields[may_publis] ?></tr>
</table>
<form method=POST action="/cgi-bin/adm/charsetfix.cgi">
<input type="hidden" name="file" value="persons">
<input name="f_key" type="hidden" value="<?php print $u->userid ?>">
<?php print "<input type=\"submit\" name=\"to-utf8\" value=\"" . _("Convert to UTF-8") . "\">" .
"<input type=\"submit\" name=\"from-utf8\" value=\"" . _("Convert from UTF-8") . "\">"; ?>
</form>
<?php
}

function showdates($u) {

?>
<?php print "<h3>" . _("Dates and times") . "</h3>\n" ?>
<table border=2 cellspacing=2>
<tr><td><?php print _("User created") ?></td><td><?php echo $u->ufields[f_ctime] ?></td></tr>
<tr><td><?php print _("User updated") ?></td><td><?php echo $u->ufields[f_mtime]
?></td></tr>
<?php
if ($u->hasperson()) {
?>
<tr><td><?php print _("Peson created") ?></td><td><?php echo $u->pfields[f_ctime] ?></td></tr>
<tr><td><?php print _("Person updated") ?></td><td><?php echo $u->pfields[f_mtime]
?></td></tr>
<?
}
?>
<tr><td><?php print _("Last login") ?></td><td><?php echo $u->ufields[logintime] ?></td></tr>
<tr><td><?php print _("Last email") ?></td><td><?php echo $u->ufields[emailtime] ?></td></tr>
</table>
<?php
}

function showemails($u) {

    $key = $u->userid;
    $query = mysql_query("select f_key, email.usage, state, level, error from email where owner = $key");
    if ($query == 0) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for emails failed");
    }
    print _("Contact") . ": " . $u->ufields[email] . "<br>\n";
    if ($u->hasperson()) {
        print _("Public") . ": " . $u->pfields[email] . "\n";
    }
    print "<table border>\n";
    print "<tr><th>" . _("Use") . "</th>\n<th>" . _("Email") . "</th>\n<th>" . _("State") . "</th>\n<th>" . _("Level") . "</th>\n<th>" . _("Error msg") . "</th>\n</tr>\n";
    while ($arr = mysql_fetch_array($query)) {
	print "<tr>\n<td>";
	print _("$arr[usage]") . "</td>\n";
	print "<td>" . $arr[f_key] . "</td>\n";
	print "<td>" . $arr[state] . "</td>\n";
	if ($arr[state] <> "ok") {
	    print "<td>" . $arr[level] . "</td>\n";
	    print "<td>" . $arr[error] . "</td>\n";
        }
	print "</tr>\n";
    }
    print "</table>\n";
}

function showplace($u) {
   $placeid = $u->pcfields[placeid];

    $query = mysql_query("select f_key from places where name = '$placeid'");
    if (!$query) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for placekey from placeid failed");
    }
	
    $arr = mysql_fetch_row($query);
    $id = $arr[0];

   $pl = cleanfields("places", $id);
   $pname = $pl[longname];
   while ($pl[within] > 1) { # skip The World (1)
	$pname .= ", ";
	$pl = cleanfields("places", $pl[within]);
	$pname .= $pl[longname];
   }
   print "<table border=\"2\" cellspacing=\"2\">\n";
   print "<tr>\n<td>" . _("Place") . "</td>\n<td>" . $pname . "</td>\n<td>" . $placeid . "</td>\n</tr>\n";
   print "<tr>\n<th>" . _("Component") . "</th>\n<th>" . _("Entered") . "</th>\n<th>" . _("Interpreted") . "</th>\n</tr>\n";
   print "<tr>\n<td>" . _("Country") . "</td>\n<td>" . $u->pfields[country]
	              . "</td>\n<td>" . $u->pcfields[country] . "</td>\n</tr>\n";
   print "<tr>\n<td>" . _("State/province/department") . "</td>\n<td>" . $u->pfields[state]
	              . "</td>\n<td>" . $u->pcfields[state] . "</td>\n</tr>\n";
   print "<tr>\n<td>" . _("City") . "</td>\n<td>" . $u->pfields[city]
	              . "</td>\n<td>" . $u->pcfields[city] . "</td>\n</tr>\n";
   print "</table>\n";
}

function showfrozen($u)
{
   print "<h3>" . _("Frozen records") . "</h3>\n";
    $key = $u->userid;
    $query = mysql_query("select f_key from frozen where owner = $key");
    if ($query == 0) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for frozen records failed");
    }
    print "<table border=\"2\" cellspacing=\"2\">\n";
    print "<tr>\n<th>" . _("Table") . "</th>\n<th>" . _("Record") . "</th>\n</tr>\n";
    while ($arr = mysql_fetch_array($query)) {
        $rec = rawfields("frozen", $arr[0]);
	print "<tr>\n<td>";
	print $rec[ftable];
	$text = preg_replace("/\\\\n/", "<br>", $rec[textform]);
	print "</td>\n<td>" . $text;
	print "</td>\n</tr>\n";
    }
    print "</table>\n";

}
 
function showmaintainedplaces($u)
{
  print "<h3>" . _("Places he maintains") . "</h3>\n";
  $key = $u->userid;
  $query = mysql_query("select f_key, name, longname from places, link_place_maintainer where places.f_key = link_place_maintainer.fromkey and link_place_maintainer.tokey = $key");
  if ($query == 0) {
    print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
    die("Query for maintained places failed");
  }
  print "<table border=\"2\" cellspacing=\"2\">\n";
  while ($arr = mysql_fetch_array($query)) {
    print "<tr>\n<td>" . $arr[0] . "</td>\n<td>" . $arr[1] . "</td>\n<td>" . $arr[2] . "</td>\n</tr>\n";
  }
  print "</table>\n";
}

?>
